package com.b2c.web;


import com.b2c.common.enums.ExceptionEnums;
import com.b2c.common.exception.MrException;
import com.b2c.common.utils.PageResult;
import com.b2c.service.BrandService;
import com.mr.service.pojo.BrandEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService service;

    @GetMapping("page")
    public ResponseEntity<PageResult> getBrandPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key" , required = false) String key,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ){
        PageResult<BrandEntity>  pageResult = service.getBrandPage(page,rows,key,sortBy,desc);

        if (pageResult==null || pageResult.getItems().size()==0){
            throw new MrException(ExceptionEnums.BRAND_CANNOT_BE_NULL);
        }
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("cid/{cid}")
    public ResponseEntity<List<BrandEntity>> queryBrandByCategory(@PathVariable("cid") Long cid) {
        List<BrandEntity> list = this.service.queryBrandByCid(cid);
        if(list == null){
            new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

    //品牌增加
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandEntity entity,@RequestParam("cids") List<Long> cids){
        service.addBrand(entity,cids);

        return ResponseEntity.ok(null);
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandEntity entity,@RequestParam("cids") List<Long> cids){
        service.updateBrand(entity ,cids);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping
    public  ResponseEntity deleteBrand(@RequestParam(value = "bid") Long bid){

            service.deleteBrand(bid);

        return ResponseEntity.ok(null);
    }

    /**
     * 根据品牌id集合查询品牌
     * @param  id
     * @return 品牌对象
     */
    @GetMapping("queryBrandById")
    public  ResponseEntity<BrandEntity> queryBrandById(@RequestParam("id") Long id){
        BrandEntity brand= service.queryBrandById(id);
        return ResponseEntity.ok(brand);
    }



}